var oprateUrl = DOMAIN + '/v1/roles' + isLogin;
$(document).ready(function () {
	loadTable();
	//查询
	$("#searchBtn").click(function () {
		//新查询参数
		function queryParamNew(params) {
			var params = {};
			params.code = $("#code").val();
			params.name = $("#name").val();
			return params;
		};
		$('#table').bootstrapTable(
			'refreshOptions', {
				url: oprateUrl,
				//查询参数
				queryParams: queryParamNew
			}
		);

	});
});

$("#Btn-insert").click(function () {
	document.getElementById("form-modal").reset();
});


function loadTable() {
	$('#table').bootstrapTable({
		url: oprateUrl,
		method: 'get',
		dataType: "json",
		contentType: "application/x-www-form-urlencoded",
		toolbar: '#toolbar',
		striped: true,
		cache: false,
		pagination: true,
		sortable: false,
		sortOrder: "asc",
		showRefresh: false,
		showColumns: false,
		minimumCountColumns: 4,
		search: false,
		queryParams: queryParams,
		toolbar: '#exampleTableEventsToolbar',
		sidePagination: "client",
		pageNumber: 1,
		pageSize: 50,
		pageList: [50, 100],
		strictSearch: true,
		clickToSelect: true,
		cardView: false,
		detailView: false,
		columns: [{
			field: 'code',
			title: "代码",
			align: "center",
			valign: "middle"
		}, {
			field: "name",
			title: "名称",
			align: "center",
			valign: "middle"
		}, {
			field: "comments",
			title: "备注",
			align: "center",
			valign: "middle"
		}, {
			field: "",
			title: "操作",
			align: "center",
			valign: "middle",
			events: funOprate,
			formatter: function (value, row, index) {
				return '<button data-toggle="modal" data-edit="div-modal" data-target="#div-modal" onclick="showBox();" class="btn btn-primary btn-sm edit">&emsp;编辑</button> <button data-del="' + oprateUrl + '" class="btn btn-primary btn-sm del">&emsp;删除</button>';
			}
		}],
		onLoadSuccess: function () {
			console.log("加载成功");
		},
		onLoadError: function () {
			console.log("加载数据失败");
		},
		//>>>>>>>>>>>>>>导出excel表格设置
		showExport: true,
		showExport: true,
		//是否显示导出按钮
		buttonsAlign: "left",
		//按钮位置
		exportDataType: 'all',
		//导出的方式 all全部 selected已选择的  basic', 'all', 'selected'.
		Icons: 'glyphicon glyphicon-export',
		exportTypes: ['excel', 'doc', 'xlsx', 'txt',],
		//导出文件类型
		exportOptions: {       //ignoreColumn: [0,1],  //忽略某一列的索引

			fileName: '导出数据表',
			//文件名称设置
			worksheetName: 'sheet1',
			//表格工作区名称
			tableName: '导出数据表',
			// excelstyles: ['background-color', 'color', 'font-size', 'font-weight'], 设置格式

		},
		//导出excel表格设置<<<<<<<<<<<<<<<<
	});
	//查询参数
	function queryParams(params) {
		var params = {};
		return params;
	};
};
//保存
function funSave() {
	var id = $("#id").val();
	var type = "";
	if (isNotEmpty(id)) {//修改
		type = "PATCH";
	} else {//新增
		//获取编号
		var code = getGeneratedCode("getMaxRoles");
		$("#pcode").val(code);
		type = "POST";
	}
	var data = $("#form-modal").serialize();
	$.ajax({
		type: type,
		url: oprateUrl,
		data: data,
		// async: false,
		success: function (result) {
			if (result.status == 200) {
				alert("保存成功！");
				$('.close').click();
				document.getElementById("form-modal").reset();
				$("#id").val("");
				$("#table").bootstrapTable("refresh", {
					silent: true //静态刷新
				});
			}
		},
		error: function () {
			alert("修改失败！");
		}
	});
};
//保存
$("#btn-save").click(function () {
	if(isEmpty($("#pname").val())){
		alert("用户组名称不能为空!");
		return;
	}
	funSave();
});

//关闭
$("#btn-close").click(function () {
	document.getElementById("form-modal").reset();
	$("#id").val("");
});

$("#Btn-insert").click(function() {
     $("#form-modal")[0].reset();
     $("#codeBox").css("display","none");
     $("#modalTitle").text("新增");
});

function showBox(){
     $("#codeBox").css("display","block");
     $("#modalTitle").text("修改");
}
